package com.centling.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.xml.namespace.QName;

import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centling.business.OrderVO;
import com.centling.dao.ICustomerDAO;
import com.centling.dao.IOrderDAO;
import com.centling.dao.IUtilsDAO;
import com.centling.service.IAxis2Service;

/**
 * 与“大后台”交互的接口
 * 
 * 此servicer中所包含的接口：
 * 
 * 1.1	订单信息取得接口(getOrdersFromBL)
 * 
 * @author John.Ju
 * 
 */
@Service("axis2Service")
@Transactional
public class Axis2Service implements IAxis2Service {

	Logger	log	= Logger.getLogger(Axis2Service.class);

	@Resource
	private ICustomerDAO customerDAO;
	
	@Resource
	private IOrderDAO orderDAO;
	
	@Resource
	private IUtilsDAO utilsDAO;
	
	/**
	 * 1.1	订单信息取得接口(getOrdersFromBL)
	 * 
	 * @param beginDate	开始时间	格式：yyyy-MM-dd
	 * @param endDate	结束时间	格式：yyyy-MM-dd
	 * @param ordersNo	
	 * @return
	 */
	@Transactional
	@Override
	public Map<String,Object> getOrdersFromBL(String beginDate, String endDate,
			String ordersNo) {
		log.info("getOrdersFromBL function start...");
		String returnStr = "";
		Map<String,Object> map = new HashMap<String,Object>();
		List<OrderVO> volist = new ArrayList<OrderVO>();
		List<String[]> orderstatuslist = new ArrayList<String[]>();
		try {
			// 获得客户端
			RPCServiceClient serviceClient = new RPCServiceClient();
			// 可以在该对象中设置服务端的验证信息
			Options options = serviceClient.getOptions();
			options.setTimeOutInMilliSeconds(1000 * 60 * 3);
			// EndpointReference targetEPR = new
			EndpointReference targetEPR = new EndpointReference(
					"http://services.rcmtm.com/services/BxppService?wsdl");
			options.setTo(targetEPR);
			QName opAddEntry = new QName("http://services.redcollar.cn/", "getCRMOrders");
			Object[] params = new Object[5] ;
			params[0] = beginDate;
			params[1] = endDate;
			params[2] = ordersNo;
			returnStr = (String) serviceClient.invokeBlocking(opAddEntry, params, new Class[] { String.class })[0];
			serviceClient.cleanupTransport();// 防止连接超时
			JSONObject jo = JSONObject.parseObject(returnStr);
			JSONArray ja = JSONArray.parseArray(jo.get("orders").toString());
			for (int i=0; i<ja.size(); i++){
				JSONObject joi = ja.getJSONObject(i);
				OrderVO vo = new OrderVO();
				//vo.setFcurrencyid();
				//vo.setFexchangerate();
				vo.setFtotalamountfor(joi.get("sumMoney")==null?"":joi.get("sumMoney").toString());
				//vo.setFtotalamount();
				vo.setF_customer_source(joi.get("formSystem")==null?"":joi.get("formSystem").toString());
				vo.setF_order_number(joi.get("orderNo")==null?"":joi.get("orderNo").toString());
				vo.setF_system_id(joi.get("systemNo")==null?"":joi.get("systemNo").toString());
				vo.setF_user_name(joi.get("username")==null?"":joi.get("username").toString());
				vo.setF_customer_number(joi.get("customerNo")==null?"":joi.get("customerNo").toString());
				vo.setF_product(joi.get("product")==null?"":joi.get("product").toString());
				vo.setF_fabric(joi.get("fabricNo")==null?"":joi.get("fabricNo").toString());
				vo.setF_date(joi.get("createTime")==null?"":joi.get("createTime").toString());
				vo.setF_deliver_date(joi.get("deliverTime")==null?"":joi.get("deliverTime").toString());
				vo.setF_delivery_date(joi.get("deliveryTime")==null?"":joi.get("deliveryTime").toString());
				//vo.setF_delivery_apply();
				vo.setF_order_status(joi.get("status")==null?"":joi.get("status").toString());
				vo.setF_pay_status(joi.get("paySatus")==null?"":joi.get("paySatus").toString());
				vo.setfName(joi.get("name")==null?"":joi.get("name").toString());
				vo.setFsex(joi.get("sex")==null?"":joi.get("sex").toString());
				vo.setFphone(joi.get("phone")==null?"":joi.get("phone").toString());
				vo.setFnote(joi.get("memo")==null?"":joi.get("memo").toString());
				//vo.setFcontractno();
				//vo.setFempoyee();
				//vo.setF_customer_material();
				//vo.setF_sales_type();
				//vo.setFcurrencyid();
				volist.add(vo);
			}
			/**
			 * 第一步处理：	处理修改
			 */
			if (jo.get("ordersStatus")!=null&&jo.get("ordersStatus").toString().trim().length()!=0){
				String[] ordersStatus = jo.get("ordersStatus").toString().trim().split(",");
				for (String temp : ordersStatus){
					String[] os = temp.split(":");
					orderstatuslist.add(os);
 				}
			}
			map.put("OrderVOList", volist);
			map.put("OrderStatusList", orderstatuslist);
		} catch (Exception e) {
			log.error("exception..., error: " + e.getMessage());
		}
		log.info("getOrdersFromBL function end...");
		return map;
	}
}
